package com.tjetc.controller;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.tjetc.common.JsonResult;
import com.tjetc.entity.ShoppingCart;
import com.tjetc.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /***
     * 增加购物车信息
     * @param shoppingCart
     * @return
     */
    @RequestMapping("add")
    public JsonResult add(ShoppingCart shoppingCart){
        int i = shoppingCartService.add(shoppingCart);
        if(i>=1){
            return new JsonResult(0,"插入成功",i);
        } else{
            return new JsonResult(1,"插入失败",i);
        }
    }

    /***
     * 更改购物车菜品数量
     * @param id
     * @param increment
     * @return
     */
    @RequestMapping("update")
    public JsonResult updateNumber(@RequestParam("id")Integer id, @RequestParam("increment")Integer increment){
        int i = shoppingCartService.updateNumber(id,increment);
        if(i>=1){
            return new JsonResult(0,"更新成功",i);
        } else{
            return new JsonResult(1,"最少选择一件",i);
        }
    }

    /***
     * 根据id删除购物车信息
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public JsonResult delete(@PathVariable Integer id){
        int i = shoppingCartService.delete(id);
        if(i>=1){
            return new JsonResult(0,"删除成功",i);
        } else{
            return new JsonResult(1,"删除失败",i);
        }
    }

    /***
     * 根据id查询购物车信息
     * @param id
     * @return
     */
    @RequestMapping("select/{id}")
    public JsonResult select(@PathVariable Integer id){
        ShoppingCart shoppingCart = shoppingCartService.select(id);
        if(shoppingCart!=null){
            return new JsonResult(0,"查询成功",shoppingCart);
        } else{
            return new JsonResult(1,"查询失败",shoppingCart);
        }
    }

    /***
     * 删除用户在某商家界面的购物车信息
     * @param userId
     * @param merchantId
     * @return
     */
    @RequestMapping("deleteAll/{userId}/{merchantId}")
    public JsonResult deleteAll(@PathVariable Integer userId, @PathVariable Integer merchantId){
        int i = shoppingCartService.deleteAll(userId, merchantId);
        if(i>=1){
            return new JsonResult(0,"删除成功",i);
        } else{
            return new JsonResult(1,"购物车中无菜品",i);
        }
    }

    /***
     * 查询用户在某商家页面所有购物车信息
     * @param userId
     * @param merchantId
     * @return
     */
    @RequestMapping("selectAll/{userId}/{merchantId}")
    public JsonResult selectAll(@PathVariable Integer userId, @PathVariable Integer merchantId){
        List<ShoppingCart> shoppingCarts = shoppingCartService.selectAll(userId, merchantId);
        if(shoppingCarts!=null){
            return new JsonResult(0,"查询成功",shoppingCarts);
        } else{
            return new JsonResult(1,"查询失败",shoppingCarts);
        }
    }

    /***
     * 更新购物车信息
     * @param userId
     * @param dishId
     * @param merchantId
     * @param increment
     * @return
     */
    @RequestMapping("updateById")
    public JsonResult selectAll(@RequestParam("userId")Integer userId,
                                @RequestParam("dishId")Integer dishId,
                                @RequestParam("merchantId")Integer merchantId,
                                @RequestParam("increment")Integer increment){
        int i = shoppingCartService.insertByShoppingCart(userId, dishId, merchantId, increment);
        if(i>=0){
            return new JsonResult(0,"更新成功",i);
        } else{
            return new JsonResult(1,"更新失败",i);
        }
    }


}
